package com.example.abe.diandi.trends.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.BindView;
import com.example.OnClick;
import com.example.abe.diandi.R;
import com.example.abe.diandi.base.BaseActivity;
import com.example.abe.diandi.category.model.CategoryModel;
import com.example.abe.diandi.main.AppConfig;
import com.example.abe.diandi.nohttp.ApiUtil;
import com.example.abe.diandi.nohttp.CallServer;
import com.example.abe.diandi.nohttp.HttpListener;
import com.example.abe.diandi.trends.fragment.ITrendEditor;
import com.example.abe.diandi.trends.fragment.TrendsEditImageFragment;
import com.example.abe.diandi.trends.fragment.TrendsEditTextFragment;
import com.example.abe.diandi.trends.view.TrendsAddFeelPopupWindow;
import com.example.abe.diandi.trends.view.TrendsSortPopupWindow;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import example.abe.com.framework.util.TimeUtil;
import example.abe.com.framework.util.ToastUtil;

import static com.example.abe.diandi.nohttp.ApiUtil.METHOD;
import static com.example.abe.diandi.nohttp.ApiUtil.M_TREND_ADD_TRENDS;
import static com.example.abe.diandi.nohttp.ApiUtil.SERVICE;
import static com.example.abe.diandi.nohttp.ApiUtil.S_TREND;

public class TrendsEditActivity extends BaseActivity {

    public static final int EDIT_IMAGE = 1000;
    public static final int EDIT_TEXT = 1001;
    public static final String EDIT_TYPE = "edit_type";

    @BindView(R.id.act_trends_edit_iv_feel)
    protected ImageView mIvFeel;

    @BindView(R.id.act_trends_edit_fl_feel)
    protected FrameLayout mFlFeel;

    @BindView(R.id.act_trends_edit_tv_title)
    protected TextView mTvTitle;

    @BindView(R.id.act_trends_edit_et_title)
    protected EditText mEtTitle;

    @BindView(R.id.act_trends_edit_fl_content)
    protected FrameLayout mFlContent;

    @BindView(R.id.act_trends_edit_tv_category)
    protected TextView mTvCategory;

    private TrendsSortPopupWindow mSortPopupWindow;
    private TrendsAddFeelPopupWindow mAddFeelPopupWindow;
    private ITrendEditor mTrendEditor;
    private int mEditType;
    private CategoryModel mCategoryModel;

    @Override
    public int getLayoutID() {
        return R.layout.activity_trends_edit;
    }

    @Override
    public void initData() {
        mEditType = getIntent().getIntExtra(EDIT_TYPE, EDIT_TEXT);
    }

    @Override
    public void initView() {
        mSortPopupWindow = new TrendsSortPopupWindow(this, AppConfig.getCategoryList());
        mSortPopupWindow.setOnItemClickListener(onSortClickListener);
        onSortClickListener.onItemClick(AppConfig.getCategoryList().get(0));
        mAddFeelPopupWindow = new TrendsAddFeelPopupWindow(this);
        mAddFeelPopupWindow.setOnItemClickListener(onAddFeelPopupWindowClickListener);
        mTvTitle.setText(TimeUtil.getStrCurrentCH("yyyy年MM月dd日"));

        if (mEditType == EDIT_TEXT) {
            TrendsEditTextFragment fragment = TrendsEditTextFragment.instance();
            mTrendEditor = fragment;
            replaceFragment(mFlContent, fragment);
        } else if (mEditType == EDIT_IMAGE) {
            TrendsEditImageFragment fragment = TrendsEditImageFragment.instance();
            mTrendEditor = fragment;
            replaceFragment(mFlContent, fragment);
        }
    }

    @Override
    public void onSideSlipEvent(){
        finish();
    }

    private TrendsSortPopupWindow.OnItemClickListener onSortClickListener = new TrendsSortPopupWindow.OnItemClickListener() {
        @Override
        public void onItemClick(CategoryModel model) {
            mCategoryModel = model;
            mTvCategory.setText(model.getTitle());
        }
    };

    private TrendsAddFeelPopupWindow.OnItemClickListener onAddFeelPopupWindowClickListener = new TrendsAddFeelPopupWindow.OnItemClickListener() {
        @Override
        public void onItemClick(TrendsAddFeelPopupWindow.FeelType type) {
            switch (type) {
                case SO_BAD:
                    mIvFeel.setImageResource(R.drawable.ic_edit_trends_feel_so_bad);
                    break;
                case BAD:
                    mIvFeel.setImageResource(R.drawable.ic_edit_trends_feel_bad);
                    break;
                case SO_SO:
                    mIvFeel.setImageResource(R.drawable.ic_edit_trends_feel_so_so);
                    break;
                case HAPPY:
                    mIvFeel.setImageResource(R.drawable.ic_edit_trends_feel_happy);
                    break;
                case SO_HAPPY:
                    mIvFeel.setImageResource(R.drawable.ic_edit_trends_feel_so_happy);
                    break;
            }
        }
    };

    @OnClick(R.id.act_trends_edit_tv_category)
    protected void onCategoryClick(View v) {
        mSortPopupWindow.showAsDropDown(v);
    }

    @OnClick(R.id.act_trends_edit_fl_feel)
    protected void onAddFeelClick(View v) {
        mAddFeelPopupWindow.showAsDropDown(v);
    }

    @OnClick(R.id.act_trends_edit_tv_cancel)
    protected void onCancelClick(View v) {
        finish();
    }

    @OnClick(R.id.act_trends_edit_tv_save)
    protected void onSaveClick(View v) {
        if (!checkEditComplete()) {
            return;
        }

        Request<String> request = NoHttp.createStringRequest(ApiUtil.URL, RequestMethod.POST);
        request.add(SERVICE, S_TREND)
                .add(METHOD, M_TREND_ADD_TRENDS)
                .add("title", mEtTitle.getText().toString())
                .add("content", mTrendEditor.getTextContent())
                .add("imageTextData", new Gson().toJson(mTrendEditor.getImageTextModelList()))
                .add("categoryId", mCategoryModel.getId())
                .add("userId", AppConfig.getUserModel().getId());
        CallServer.getInstance().add(this, request, new HttpListener<String>() {

            @Override
            public void onSucceed(int what, Response<String> response) {
                JsonObject jsonObj = new JsonParser().parse(response.get()).getAsJsonObject();
                Boolean isSuccess = jsonObj.get("success").getAsBoolean();
                if (isSuccess) {
                    ToastUtil.makeText("保存成功");
                    finish();
                } else {
                    ToastUtil.makeText("保存失败");
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {
                ToastUtil.makeText("保存失败");
            }
        });
    }

    private boolean checkEditComplete() {
        if (TextUtils.isEmpty(mEtTitle.getText().toString())) {
            ToastUtil.makeText("标题为空");
            return false;
        }
        if (mCategoryModel == null) {
            ToastUtil.makeText("未选择类别");
            return false;
        }
        if (mTrendEditor.getTextContent() == null && mTrendEditor.getImageTextModelList() == null) {
            ToastUtil.makeText("内容为空");
            return false;
        }
        return true;
    }
}